package part15;

import java.util.EnumSet;
import java.util.HashSet;
import java.util.Set;

/**
 * @author wcj
 * @Date 2021/5/29 17:31
 * @Version 1.0
 */
public class Part1513 {

    /**
     * 泛型实现Set的工具函数并集，交集，差集等
     *
     * @param args
     */
    public static void main(String[] args) {
        Set<WaterColors> set1 = EnumSet.range(WaterColors.BRILLIANT_RED, WaterColors.VIRIDIAN_HUE);
        Set<WaterColors> set2 = EnumSet.range(WaterColors.CERULEAN_BLUE_HUE, WaterColors.VIRIDIAN_HUE);
        System.out.println(MathUtil.union(set1, set2));
        System.out.println(MathUtil.intersection(set1, set2));
        System.out.println(MathUtil.diff(set1, set2));
        System.out.println(MathUtil.complement(set1, set2));
    }

}

enum WaterColors {
    ZINC, LEMON_YELLOW, MEDIUM_YELLOW, DEEP_YELLOW, ORANGE,
    BRILLIANT_RED, CRIMSON, MAGENTA, ROSE_MADDER, VIOLET,
    CERULEAN_BLUE_HUE, PHTHALO_BLUE, ULTRAMARINE, COBALT_BLUE_HUE,
    PERMANENT_GREEN, VIRIDIAN_HUE,
}

class MathUtil {
    /**
     * 并集
     *
     * @param a
     * @param b
     * @param <T>
     * @return
     */
    public static <T> Set<T> union(Set<T> a, Set<T> b) {
        HashSet<T> result = new HashSet<>(a);
        result.addAll(b);
        return result;
    }

    /**
     * 交集
     *
     * @param a
     * @param b
     * @param <T>
     * @return
     */
    public static <T> Set<T> intersection(Set<T> a, Set<T> b) {
        HashSet<T> result = new HashSet<>(a);
        result.retainAll(b);
        return result;
    }

    /**
     * 差集
     *
     * @param a
     * @param b
     * @param <T>
     * @return
     */
    public static <T> Set<T> diff(Set<T> a, Set<T> b) {
        HashSet<T> result = new HashSet<>(a);
        result.removeAll(b);
        return result;
    }

    /**
     * 包含交集以外的所有元素
     *
     * @param a
     * @param b
     * @param <T>
     * @return
     */
    public static <T> Set<T> complement(Set<T> a, Set<T> b) {
        return diff(union(a, b), intersection(a, b));
    }
}
